package com.test.javafaq_portal.mapper;

import com.test.javafaq_portal.entity.Permission;
import com.test.javafaq_portal.entity.Role;
import com.test.javafaq_portal.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.javafaq_portal.vo.UserVo;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* <p>
    *  Mapper 接口
    * </p>
*
* @author test.com
* @since 2025-04-11
*/
@Repository
public interface UserMapper extends BaseMapper<User> {


    @Select("SELECT * FROM user u  LEFT JOIN user_role ur ON u.id=ur.user_id LEFT JOIN role r ON r.id=ur.role_id WHERE u.id=#{userId}")
    List<Role> findUserRolesById(Integer userId);
    @Select("select id,username,nickname from user where username=#{username}")
    UserVo findUserVoByUsername(String username);
    @Select("SELECT p.id,p.name " +
            "FROM user u " +
            "LEFT JOIN user_role ur ON u.id=ur.user_id " +
            "LEFT JOIN role r ON r.id=ur.role_id " +
            "LEFT JOIN role_permission rp ON r.id=rp.role_id " +
            "LEFT JOIN permission p ON p.id=rp.permission_id " +
            "WHERE u.id=#{id}")
    List<Permission> findUserPermissionById(Integer id);

    //按用户名查询用户
    @Select("SELECT * FROM user WHERE username=#{username}")
    User findUserByUsername(String username);


}
